//
//  AccountTools.m
//  AppExample
//
//  Created by user on 15/10/16.
//  Copyright © 2015年 AppExample. All rights reserved.
//

#import "AccountTools.h"
#import <CommonCrypto/CommonDigest.h>

@implementation AccountTools

//转化为json串
+(NSData *)toJSONData:(id)theData
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] != 0 && error == nil){
        return jsonData;
    }else{
        return nil;
    }
}

//判断输入是否为空
+(BOOL)isEmpty:(NSString *) string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    if ([string isEqualToString:@"null"]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]) {
        return YES;
    }
    if ([string isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

+(BOOL)isLogin
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userToken = [userDefaults objectForKey:@"token"];
    if ([self isEmpty:userToken]) {
        return NO;
    }else{
        return YES;
    }
}
/**
 *  登录之后将token存入本地方便以后接口调用
 *
 *  @param token 登录之后后台返回的一串字符
 */
+(void)saveUserToken:(NSString*)token
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:token forKey:@"token"];
    [userDefaults  synchronize];
}

+(NSString*)getUserToken
{
    return [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
}
//从NSUserDefaults中删除
+ (void)removeNSUserToken
{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    [userDefaultes removeObjectForKey:@"token"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}

+ (BOOL)validatePassWord:(NSString*)PassWord
{
    if (PassWord.length>32||PassWord.length<8) {
        return NO;
    }else{
        return YES;
    }
}
+ (BOOL)validatePhoneNumber:(NSString *)string{
    
    NSString *phoneRegex = @"^(1[0-9])\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:string];
}
+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);// This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
+ (void)Savename:(NSString*)name
      userHeadPhoto:(NSString *)userHeadPhoto
     jobNumber:(NSString *)jobNumber
        userType:(NSString *)userType
{
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:name forKey:@"name"];
    [userDefaults setObject:userHeadPhoto forKey:@"userHeadPhoto"];
    [userDefaults setObject:jobNumber forKey:@"jobNumber"];
    [userDefaults setObject:userType forKey:@"userType"];
    [userDefaults synchronize];
}
+ (NSArray *)getUserInformation
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *address = [userDefaults objectForKey:@"address"]?[userDefaults objectForKey:@"address"]:@"";
    NSString *createTime =[userDefaults objectForKey:@"createTime"]?[userDefaults objectForKey:@"createTime"]:@"";
    NSString *email = [userDefaults objectForKey:@"email"]?[userDefaults objectForKey:@"email"]:@"";
    
    NSString *mobile = [userDefaults objectForKey:@"mobile"]?[userDefaults objectForKey:@"mobile"]:@"";
    NSString *position =[userDefaults objectForKey:@"position"]?[userDefaults objectForKey:@"position"]:@"";
    NSString *resultCode = [userDefaults objectForKey:@"resultCode"]?[userDefaults objectForKey:@"resultCode"]:@"";
    
    NSString *sex = [userDefaults objectForKey:@"sex"]?[userDefaults objectForKey:@"sex"]:@"";
    NSString *token =[userDefaults objectForKey:@"token"]?[userDefaults objectForKey:@"token"]:@"";
    
    
    NSString *updateTime = [userDefaults objectForKey:@"updateTime"]?[userDefaults objectForKey:@"updateTime"]:@"";
    NSString *userCd =[userDefaults objectForKey:@"userCd"]?[userDefaults objectForKey:@"userCd"]:@"";
    NSString *userHeadPhoto = [userDefaults objectForKey:@"userHeadPhoto"]?[userDefaults objectForKey:@"userHeadPhoto"]:@"";
    
    NSString *userId = [userDefaults objectForKey:@"userId"]?[userDefaults objectForKey:@"userId"]:@"";
    NSString *userName =[userDefaults objectForKey:@"userName"]?[userDefaults objectForKey:@"userName"]:@"";
    NSString *userTokenId = [userDefaults objectForKey:@"userTokenId"]?[userDefaults objectForKey:@"userTokenId"]:@"";
    
    NSString *userType = [userDefaults objectForKey:@"userType"]?[userDefaults objectForKey:@"userType"]:@"";
    NSString *zipCode =[userDefaults objectForKey:@"zipCode"]?[userDefaults objectForKey:@"zipCode"]:@"";
    
    return @[ address, createTime, email,mobile,position,resultCode,sex,token,updateTime,userCd,userHeadPhoto,userId,userName,userTokenId,userType,zipCode];

}
+ (BOOL)allIsNumber:(NSString *)string{
    NSString *numberRegex = @"^[0-9]*$";
    NSPredicate *numberTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numberRegex];
    return [numberTest evaluateWithObject:string];
}
+ (BOOL)validateEmail:(NSString *)string{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:string];
}
+ (void)exitLogin
{
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"address"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"createTime"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"email"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"mobile"];
    
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"position"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"resultCode"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"sex"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"token"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"updateTime"];
    
    
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userCd"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userHeadPhoto"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userId"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userName"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userTokenId"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"userType"];
    [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"zipCode"];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getLocalVersion
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return version;
}
+ (BOOL)isUserName:(NSString *)name
{
    NSString *      regex = @"(^[\u4e00-\u9fa5])((?!.*\n).*$)";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [pred evaluateWithObject:name];
    
}

+(BOOL)isChineseStr:(NSString*)ChineseStr
{
    
    NSString *      regex = @"^[\u4E00-\u9FA5]*$";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  
    return [pred evaluateWithObject:ChineseStr];
}


+ (BOOL)UnlimitedLengthfChineseCharacters:(NSString *)name
{
    NSString *      regex = @"(^[\u4e00-\u9fa5]*$)";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:name];
}


+(BOOL)checkIDCard:(NSString *)value
{
    value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([value length] != 18) {
        return NO;
    }
    NSString *mmdd = @"(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))";
    NSString *leapMmdd = @"0229";
    NSString *year = @"(19|20)[0-9]{2}";
    NSString *leapYear = @"(19|20)(0[48]|[2468][048]|[13579][26])";
    NSString *yearMmdd = [NSString stringWithFormat:@"%@%@", year, mmdd];
    NSString *leapyearMmdd = [NSString stringWithFormat:@"%@%@", leapYear, leapMmdd];
    NSString *yyyyMmdd = [NSString stringWithFormat:@"((%@)|(%@)|(%@))", yearMmdd, leapyearMmdd, @"20000229"];
    NSString *area = @"(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}";
    NSString *regex = [NSString stringWithFormat:@"%@%@%@", area, yyyyMmdd  , @"[0-9]{3}[0-9Xx]"];
    
    NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if (![regexTest evaluateWithObject:value]) {
        return NO;
    }
    int summary = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7
    + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9
    + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10
    + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5
    + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8
    + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4
    + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2
    + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6
    + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
    NSInteger remainder = summary % 11;
    NSString *checkBit = @"";
    NSString *checkString = @"10X98765432";
    checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
    return [checkBit isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}


+ (void)SaveUserAddress:(NSString*)address
       createTime:(NSString *)createTime
           email:(NSString *)email
                 mobile:(NSString *)mobile position:(NSString*)position  resultCode:(NSString*)resultCode  sex:(NSString*)sex token:(NSString*)token updateTime:(NSString*)updateTime userCd:(NSString*)userCd userHeadPhoto:(NSString*)userHeadPhoto  userId:(NSString*)userId userName:(NSString*)userName  userTokenId:(NSString*)userTokenId userType:(NSString*)userType zipCode:(NSString*)zipCode

{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:address forKey:@"address"];
    [userDefaults setObject:createTime forKey:@"createTime"];
    [userDefaults setObject:email forKey:@"email"];
    
    [userDefaults setObject:mobile forKey:@"mobile"];
    [userDefaults setObject:position forKey:@"position"];
    [userDefaults setObject:resultCode forKey:@"resultCode"];
    [userDefaults setObject:sex forKey:@"sex"];
    
    [userDefaults setObject:token forKey:@"token"];
    [userDefaults setObject:updateTime forKey:@"updateTime"];
    
    [userDefaults setObject:userCd forKey:@"userCd"];
    [userDefaults setObject:userHeadPhoto forKey:@"userHeadPhoto"];
    [userDefaults setObject:userId forKey:@"userId"];
    [userDefaults setObject:userName forKey:@"userName"];
    
    [userDefaults setObject:userTokenId forKey:@"userTokenId"];
    [userDefaults setObject:userType forKey:@"userType"];
    [userDefaults setObject:zipCode forKey:@"zipCode"];
    
    [userDefaults synchronize];
    
}
+(void)SaveUserToken:(NSString*)token userName:(NSString*)userName userCd:(NSString*)userCd address:(NSString*)address email:(NSString*)email zipCode:(NSString*)zipCode position:(NSString*)position sex:(NSString*)sex
{
   
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:address forKey:@"address"];
   
    [userDefaults setObject:email forKey:@"email"];
    [userDefaults setObject:position forKey:@"position"];
    
    
    [userDefaults setObject:token forKey:@"token"];
    
    [userDefaults setObject:userCd forKey:@"userCd"];
    
    [userDefaults setObject:userName forKey:@"userName"];
    
    
    [userDefaults setObject:zipCode forKey:@"zipCode"];
    [userDefaults setObject:sex forKey:@"sex"];
    
    [userDefaults synchronize];

}
@end
